package com.lihepeng.leecode.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.*;

/**
 * 多线程测试类
 */
public class MultiThread {
    public static void main(String[] args) throws InterruptedException {
        List<Integer> list = new ArrayList<>();
        for (int i=0;i<1000;i++){
            list.add(i);
        }
        List<Integer> list1 = new Vector<>();
        System.out.println("开始时间"+System.currentTimeMillis());
        ThreadPoolExecutor executor = new ThreadPoolExecutor(20,20,1000, TimeUnit.MILLISECONDS,new LinkedBlockingDeque<>());
        list.forEach(val->{
            Future<?> submit = executor.submit(new Runnable() {
                @Override
                public void run() {
                    list1.add(val);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });

        });
        executor.shutdown();
        while (true){
            if (executor.isTerminated()){
                System.out.println("结束时间"+System.currentTimeMillis());
                System.out.println(list1.size());
                break;
            }

        }

    }
}
